//
//  MusicMineViewController.m
//  FNer
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "MusicMineViewController.h"
#import "BaseTableView.h"
#import "MusicInfo.h"
#import "MusicDetailTableView.h"
#import "MusicPlayerViewController.h"
@interface MusicMineViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)BaseTableView *tableView;
@property (nonatomic, strong)CoreDataManager *coreDataManger;
@property (nonatomic, strong)NSArray *myArray;
@end

@implementation MusicMineViewController
{
    MusicPlayerViewController *play;
}


- (void)viewWillAppear:(BOOL)animated
{
    [self getData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView  = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.coreDataManger =[CoreDataManager shareManager];
    [self.view addSubview:self.tableView];
    [self getData];
}

- (void)getData
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MusicInfo" inManagedObjectContext:self.coreDataManger.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    //    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<#format string#>", <#arguments#>];
    //    [fetchRequest setPredicate:predicate];
    //    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"musicName"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    self.myArray = [self.coreDataManger.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (_myArray == nil) {
        NSLog(@"%@",error);
    }
    else
    {
        
    }
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       NSLog(@"%ld",_myArray.count);
    return _myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *celli = @"qiang";
    MusicDetailTableView *cell = [tableView dequeueReusableCellWithIdentifier:celli];
    if (nil == cell) {
        cell = [[MusicDetailTableView alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:celli];
    }
    MusicInfo *model =[self.myArray objectAtIndex:indexPath.row];
    cell.modelinfo = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     play =  [MusicPlayerViewController sharePlayerDataHandel];

    MusicInfo *model =[self.myArray objectAtIndex:indexPath.row];
 //   play.url = model.musicUrl;
    play.url = model.musicLocalUrl;
    play.allMusicArray = self.myArray;
    play.indexRow = indexPath.row;
    play.imageUrl = model.musicImage;
    play.song_id = [model.musicLocalUrl integerValue];
          [SHAREDAPPDELE.myTB isHiddenTabBar:YES withViewController:play];
   // self.moveScrollView.contentSize = CGSizeMake(400, self.moveScrollView.frame.size.height);
   // self.moveScrollView.showsHorizontalScrollIndicator = NO;
    
   // play.moveLabel.text = model.name;
    
  //  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveAction) userInfo:nil repeats:YES];
    
    
//    [self.moveScrollView addSubview:play.moveLabel];
 //   play.navigationItem.titleView =  self.moveScrollView;
    play.hidesBottomBarWhenPushed = YES;
    [play playLocalMusic];
    play.isLocal = YES;
    [self.navigationController pushViewController:play animated:YES];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
